package com.xyzq.hs.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

public class ByteUtils {

	public static byte[] short2byte(short s) {
		ByteBuffer byffer = ByteBuffer.allocate(2);
		ByteBuffer putShort = byffer.putShort(s);
		return putShort.array();
	}

	public static short byte2short(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		return buffer.getShort();
	}

	public static short byte2short2(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		return buffer.getShort();
	}

	public static byte[] int2byte(int i) {
		ByteBuffer byffer = ByteBuffer.allocate(4);
		ByteBuffer putInt = byffer.putInt(i);
		return putInt.array();
	}

	public static int byte2int(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		return buffer.getInt();
	}

	public static byte[] long2byte(int i) {
		ByteBuffer byffer = ByteBuffer.allocate(4);
		ByteBuffer putLong = byffer.putLong(i);
		return putLong.array();
	}

	public static long byte2long(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		return buffer.getLong();
	}

	public static byte[] float2byte(float f) {
		return ByteBuffer.allocate(4).putFloat(f).array();
	}

	public static float byte2float(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		return buffer.getFloat();
	}

	public static byte[] double2byte(double d) {
		return ByteBuffer.allocate(4).putDouble(d).array();
	}

	public static double byte2double(byte[] b) {
		ByteBuffer buffer = ByteBuffer.wrap(b);
		return buffer.getDouble();
	}

	public static byte[] char2byte(char[] chars) {
		Charset cs = Charset.forName("UTF-8");
		CharBuffer cb = CharBuffer.allocate(chars.length);
		cb.put(chars);
		cb.flip();
		ByteBuffer bb = cs.encode(cb);
		return bb.array();
	}

	public static char[] byte2char(byte[] bytes) {
		Charset cs = Charset.forName("UTF-8");
		ByteBuffer bb = ByteBuffer.allocate(bytes.length);
		bb.put(bytes);
		bb.flip();
		CharBuffer cb = cs.decode(bb);
		return cb.array();
	}

	public static String byte2str(byte[] bytes) {
		if(bytes[bytes.length-1]==0){
			bytes=ByteUtils.subBytes(bytes, 0, bytes.length-1);
		}
		return new String(bytes,Charset.forName("GB2312"));
	}
	
	/**
	 * CheckSum 算法 算法参考:http://www.kafan.cn/edu/4809151.html
	 * 
	 * @param bytes
	 * @return
	 */
	public static byte[] calcCheckSum(byte[] bytes) {
		int checkSum = 0, i = 0;
		for (i = 0; i < bytes.length;) {
			checkSum += ((bytes[i] & 0xFF) + (bytes[i + 1] & 0xFF) * 256);
			i += 2;
		}
		checkSum = checkSum / 65536 + checkSum % 65536;
		short result = (short) (65535 - checkSum);
		return short2byte(result);
	}

	public static void println(byte[] b) {
		System.out.println(Arrays.toString(b));
	}

	public static void println(String title, byte[] b) {
		System.out.println("[" + b.length + "] " + title + " : "
				+ Arrays.toString(b));
	}

	public static byte[] subBytes(byte[] src, int beginPos, int length) {
		byte[] dest = new byte[length];
		for (int i = beginPos, j = 0; j < length; i++, j++) {
			dest[j] = src[i];
		}
		return dest;
	}

	public static byte[] read(byte[] src, Integer beginPos, int length) {
		byte[] dest = new byte[length];
		for (int i = beginPos.intValue(), j = 0; j < length; i++, j++) {
			dest[j] = src[i];
		}
		beginPos += length;
		return dest;
	}

	public static byte[] merge(byte[]... bytes) {
		int length = 0;
		int srcPos = 0;
		int destPos = 0;
		for (byte[] b : bytes) {
			length += b.length;
		}
		byte[] dest = new byte[length];
		for (byte[] b : bytes) {
			System.arraycopy(b, srcPos, dest, destPos, b.length);
			destPos += b.length;
		}
		return dest;
	}

	public static void copy(byte[] src, int srcPos, byte[] dest, int destPos,
			int length) {
		System.arraycopy(src, srcPos, dest, destPos, length);
	}

	public static void main(String[] args) {
		// System.out.println(Arrays.toString(int2byte(1)));
		// System.out.println(Arrays.toString(char2byte(new char[] { 'K', 'V'
		// })));
		// System.out.println(Arrays.toString(byte2char(new byte[] { 75, 86
		// })));
		// System.out.println(byte2str(new byte[] { 75, 86 }));
		// byte[] src = new byte[] { 'a', 'b', 'c' };
		// byte[] src2 = new byte[] { 0, 1, 1 };
		// byte[] src3 = new byte[] { 0, 0, 1 };
		// println(src);
		// println(subBytes(src, 0, 1));
		// println(subBytes(src, 1, 1));
		// println(subBytes(src, 1, 2));
		// println(subBytes(src, 0, 3));
		//
		// println(merge(src, src2, src3));
		// Integer b=new Integer(0);
		// read(new byte[]{1,2,4},b ,2);
		// System.out.println(b);

		System.out.println(ByteUtils.byte2short2(new byte[] { 3, 0 }));
		System.out.println(ByteUtils.byte2char(new byte[] { 70 }));
		String s=new String(byte2str(new byte[] { -60, -49, -73, -67,
			-69, -7, -67, -16, 0 }));

		System.out.println(byte2int(new byte[]{0,0,14, -101}));

	}

}
